local ClientOHOH = _G.ClientOHOH;

local M = ClientOHOH.class.new(script, ClientOHOH.files.OHOHOLYBUIBase());
local CommonOHOH =_G.CommonOHOH
local CallBackManager = ClientOHOH.load.CallBackManager();
local BtnHelper = ClientOHOH.load.BtnHelper();
local LanguageHelper=_G.LanguageHelper 
local Asset_Cfg=ClientOHOH.config.Asset
local BigNumberHelper = ClientOHOH.load.BigNumberHelper();
local ConnectClass=_G.CommonOHOH.load.Connect();


function InitInsts(ui)
	ui.insts = {};
	ui.insts.CostIcon ="ctrl.price.icon"
	ui.insts.CostCount ="ctrl.price.price"

	ui.insts.list ="ctrl.petRoot"

	ui.insts.E ="ctrl.btn.E"
	ui.insts.R ="ctrl.btn.R"
	ui.insts.T ="ctrl.btn.T"
    ui.insts.Y ="ctrl.btn.Y"

	ui.insts.Lucky={
        [1]={button="ctrl.luck.1.btn",
		State="ctrl.luck.1.equip",
        luckDes="ctrl.luck.1.text",},

        [2]={button="ctrl.luck.2.btn",
		State="ctrl.luck.2.equip",
        luckDes="ctrl.luck.2.text",},

        [3]={button="ctrl.luck.3.btn",
		State="ctrl.luck.3.equip",
        luckDes="ctrl.luck.3.text",}
    }
end



function M.new(...)
	return M(...);
end;
function M.ctor(self)
	InitInsts(self)

	M.super.ctor(self,ClientOHOH:GetPath_Loop("ReplicatedStorage.Resource.UI.EggLuckGui"));

    self.HatchEvent=ConnectClass.new()
    self.LuckyBuyEvent=ConnectClass.new()


    for k,v in pairs(self.insts.Lucky) do
        CallBackManager:AddRbxConnect(self,v.button.Activated, function()
            if  self.LuckyData and  self.LuckyData[k].IsHad then
                return 
            end
            self.LuckyBuyEvent:Action(k)
        end);
    end


    CallBackManager:AddRbxConnect(self,self.insts.E.Activated, function()
        self:Hatch("E")
	end);
    CallBackManager:AddRbxConnect(self,self.insts.R.Activated, function()
        self:Hatch("R")
	end);
    CallBackManager:AddRbxConnect(self,self.insts.T.Activated, function()
        self:Hatch("T")
	end);

    CallBackManager:AddRbxConnect(self,self.insts.Y.Activated, function()
        self:Hatch("Y")
	end);

end;


function M.dtor(self)
	M.super.dtor(self);
end;

local ContextActionService = game:GetService("ContextActionService")

function M.Hatch(self,id)
    self.HatchEvent:Action(id)
end

-- {Lucky1={lucky=100,IsHad=true},Lucky2={lucky=100,IsHad=true},Lucky3={lucky=100,IsHad=true}
-- }
function M.SetLuckData(self,data)
    self.LuckyData=data
    for k,v in pairs(self.LuckyData) do
        self:FullText(self.insts.Lucky[k].luckDes,{v.Lucky})
        if v.IsHad then
            self.insts.Lucky[k].State.Visible=true
        else
            self.insts.Lucky[k].State.Visible=false
        end
    end
end

function M.GetCellParent(self)
    return self.insts.list
end


function M.OnShow(self,att,costIcon,price)

    self:BindKeyCode()
    self.main_inst.Adornee=att
    self.insts.CostIcon.Image =costIcon
    self.insts.CostCount.Text =price --LanguageHelper:GetText(70)

end;



function M.BindKeyCode(self)

    ContextActionService:BindAction("HATCH_E", function(actionName, inputState, _inputObject)
        if  actionName == "HATCH_E" and inputState==Enum.UserInputState.End then
            self:Hatch("E")
        end
       
    end, false, Enum.KeyCode.E)

    ContextActionService:BindAction("HATCH_R", function(actionName, inputState, _inputObject)
        if  actionName == "HATCH_R" and inputState==Enum.UserInputState.End then
            self:Hatch("R")
        end
       
    end, false, Enum.KeyCode.R)

    ContextActionService:BindAction("HATCH_Y", function(actionName, inputState, _inputObject)
        if  actionName == "HATCH_Y" and inputState==Enum.UserInputState.End then
            self:Hatch("Y")
        end
       
    end, false, Enum.KeyCode.Y)

    
    ContextActionService:BindAction("HATCH_T", function(actionName, inputState, _inputObject)
        if  actionName == "HATCH_T" and inputState==Enum.UserInputState.End then
            self:Hatch("T")
        end
       
    end, false, Enum.KeyCode.T)
end

function M.CloseKeyCode(self)
    ContextActionService:UnbindAction("HATCH_E")
    ContextActionService:UnbindAction("HATCH_R")
    ContextActionService:UnbindAction("HATCH_T")
    ContextActionService:UnbindAction("HATCH_Y")
end

function M.OnHide(self)
    self:CloseKeyCode()
    self.main_inst.Adornee=nil
    self.hatchId=nil
end;


return M
